package com.situ.hospital.service.impl;

import com.situ.hospital.dao.IMedicineDAO;
import com.situ.hospital.dao.IRestockingDAO;
import com.situ.hospital.dao.Impl.MedicineDAOImpl;
import com.situ.hospital.dao.Impl.RestockingDAOImpl;
import com.situ.hospital.pojo.Medicine;
import com.situ.hospital.pojo.Restocking;
import com.situ.hospital.pojo.query.RestockingQuery;
import com.situ.hospital.pojo.vo.RestockingVO;
import com.situ.hospital.service.IRestockingService;
import com.situ.hospital.util.PageResult;

import java.util.List;

public class RestockingServiceImpl implements IRestockingService {

    private IRestockingDAO restockingDAO = new RestockingDAOImpl();
    private IMedicineDAO medicineDAO = new MedicineDAOImpl();

    @Override
    public PageResult<RestockingVO> selectByPage(RestockingQuery restockingQuery) {
        List<RestockingVO> list = restockingDAO.selectByPage(restockingQuery);
        int totalCount = restockingDAO.selectTotalCount(restockingQuery);
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public void deleteById(int id) {
        Restocking restocking = restockingDAO.selectById(id);
        Medicine medicine = medicineDAO.selectById(restocking.getMedicineId());
        int inventory = medicine.getInventory() - restocking.getQuantity();
        medicineDAO.updateInventory(restocking.getMedicineId(),inventory);
        restockingDAO.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for(String id : ids) {
            Restocking restocking = restockingDAO.selectById(Integer.parseInt(id));
            Medicine medicine = medicineDAO.selectById(restocking.getMedicineId());
            int inventory = medicine.getInventory() - restocking.getQuantity();
            medicineDAO.updateInventory(restocking.getMedicineId(),inventory);
            restockingDAO.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Restocking restocking) {
        restockingDAO.add(restocking);
        Medicine medicine = medicineDAO.selectById(restocking.getMedicineId());
        int inventory = medicine.getInventory() + restocking.getQuantity();
        medicineDAO.updateInventory(restocking.getMedicineId(),inventory);
    }

    @Override
    public Restocking selectById(int id) {
        return restockingDAO.selectById(id);
    }

    @Override
    public void update(Restocking restocking) {
        Restocking oldRestocking = restockingDAO.selectById(restocking.getId());
        Medicine oldMedicine = medicineDAO.selectById(oldRestocking.getMedicineId());
        int oldInventory = oldMedicine.getInventory() - oldRestocking.getQuantity();
        medicineDAO.updateInventory(oldMedicine.getId(),oldInventory);
        Medicine newMedicine = medicineDAO.selectById(oldRestocking.getMedicineId());
        int newInventory = newMedicine.getInventory() + restocking.getQuantity();
        medicineDAO.updateInventory(newMedicine.getId(),newInventory);
        restockingDAO.update(restocking);
    }
}
